package me.android.library.utils.android;

import android.Manifest;
import android.annotation.SuppressLint;
import android.telephony.CellInfo;
import android.telephony.CellInfoCdma;
import android.telephony.CellInfoGsm;
import android.telephony.CellInfoLte;
import android.telephony.CellInfoNr;
import android.telephony.CellInfoWcdma;
import android.telephony.TelephonyManager;

import androidx.annotation.RequiresPermission;

import java.util.List;

import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import mylab.utils.common.StringUtil;


@Slf4j
@UtilityClass
public class TelephonyUtil {

    @RequiresPermission(allOf = {Manifest.permission.READ_SMS, Manifest.permission.READ_PHONE_NUMBERS, Manifest.permission.READ_PHONE_STATE})
    @SuppressLint("HardwareIds")
    public String getPhoneNumber() {
        return SystemServiceUtil.getTelephonyManager().getLine1Number();
    }

    public String getImei() {
        TelephonyManager tm = SystemServiceUtil.getTelephonyManager();
        String imei = tm.getImei();
        return StringUtil.isNotEmpty(imei) ? imei : tm.getMeid();
    }

    @RequiresPermission(allOf = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.ACCESS_FINE_LOCATION})
    @SuppressLint("HardwareIds")
    public String getTelephonyProperties() {
        TelephonyManager tm = SystemServiceUtil.getTelephonyManager();
        String RN = System.lineSeparator();

        //@formatter:off

        StringBuilder sb = new StringBuilder();
        sb.append("telephony properties:").append(RN);
        sb.append("====================:").append(RN);
        sb.append(String.format("getCallState: %s", tm.getCallState())).append(RN);
        sb.append(String.format("getDataActivity: %s", tm.getDataActivity())).append(RN);
        sb.append(String.format("getDataState: %s", tm.getDataState())).append(RN);
        sb.append(String.format("getIMEI: %s", getImei())).append(RN);
        sb.append(String.format("getDeviceSoftwareVersion: %s", tm.getDeviceSoftwareVersion())).append(RN);
        sb.append(String.format("getLine1Number: %s", tm.getLine1Number())).append(RN);
        sb.append(String.format("getNetworkCountryIso: %s", tm.getNetworkCountryIso())).append(RN);
        sb.append(String.format("getNetworkOperator: %s", tm.getNetworkOperator())).append(RN);
        sb.append(String.format("getNetworkOperatorName: %s", tm.getNetworkOperatorName())).append(RN);
        sb.append(String.format("getNetworkType: %s", tm.getNetworkType())).append(RN);
        sb.append(String.format("getPhoneType: %s", tm.getPhoneType())).append(RN);
        sb.append(String.format("getSimCountryIso: %s", tm.getSimCountryIso())).append(RN);
        sb.append(String.format("getSimOperator: %s", tm.getSimOperator())).append(RN);
        sb.append(String.format("getSimOperatorName: %s", tm.getSimOperatorName())).append(RN);
        sb.append(String.format("getSimState: %s", tm.getSimState())).append(RN);
        sb.append(String.format("getSubscriberId: %s", tm.getSubscriberId())).append(RN);
        sb.append(String.format("getVoiceMailAlphaTag: %s", tm.getVoiceMailAlphaTag())).append(RN);
        sb.append(String.format("getVoiceMailNumber: %s", tm.getVoiceMailNumber())).append(RN);

        sb.append("").append(RN);
        sb.append("telephony cellLocation:").append(RN);
        sb.append("====================:").append(RN);

        List<CellInfo> cells = tm.getAllCellInfo();
        for (CellInfo cell : cells) {
            // 根据网络类型处理不同的 CellInfo 子类
            if (cell instanceof CellInfoGsm gsm) {
                // 处理 GSM 网络信息
                sb.append(String.format("gsmCell: %s", gsm)).append(RN);
            } else if (cell instanceof CellInfoCdma cdma ) {
                // 处理 CDMA 网络信息
                sb.append(String.format("cdmaCell: %s", cdma)).append(RN);
            } else if (cell instanceof CellInfoLte lte) {
                // 处理 LTE 网络信息
                sb.append(String.format("lteCell: %s", lte)).append(RN);
            } else if (cell instanceof CellInfoWcdma wcdma) {
                // 处理 WCDMA 网络信息
                sb.append(String.format("wcdmaCell: %s", wcdma)).append(RN);
            } else if (cell instanceof CellInfoNr nr) {
                // 处理 5G NR 网络信息（Android 10+）
                sb.append(String.format("nrCell: %s", nr)).append(RN);
            }
        }

        //@formatter:on

        return sb.toString();
    }

}
